<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use \Common\Service\OrdersService;
/**
 * 模型数据管理控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class OrdersController extends AdminController {
    protected $model;
    
	protected function _initialize(){
	    parent::_initialize();
	    
	    $this->model = D('Orders','Service');
	}
    /**
     * 显示指定模型列表数据
     * @param  String $model 模型标识
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $prefix = C('DB_PREFIX');
        $model      =   M()->table($prefix.'orders o');

        $map = [];

        $nickname = trim(I('nickname'));
        if(!empty($nickname)){
            $map['m.nickname'] = ['like', '%'.(string)$nickname.'%'];
        }

        $ordersn = trim(I('sn'));
        if(!empty($ordersn)){
            $map['o.sn'] = ['like', '%'.(string)$ordersn.'%'];
        }

        $status = I('status');

        if(!empty($status)){
            $map['o.status'] = $status;
        }

        $model->join($prefix.'wx_user m on m.userid = o.userid','LEFT');

        $this->assign('_list',$this->lists($model,$map,'o.create_time desc','o.*,m.nickname'));

        $this->meta_title = '订单列表';
        $this->display();
    }
    
    public function detail(){
        $sn = I('get.sn','');
        $item = $this->model->detail(array('o.sn'=>$sn));

	    if(empty($item)){
	        $this->error('找不到对应的订单！');
	    }
	    
	    if(IS_POST){
	        $this->_saveOrder($item);
	    }
	    $item['products'] = M('order_product')->where(array('sn'=>$item['sn']))->select();
	    $this->assign('item',$item);
	    
	    $this->meta_title = '订单详情';
        $this->display();
    }
    
    private function _saveOrder($item){
        
        if($item['status'] == OrdersService::STATUS_WATI_PAY && $item['pay_status'] == OrdersService::PAY_STATUS_NO_PAY){
             $pay_status = I('post.pay_status');
             if($pay_status != OrdersService::PAY_STATUS_PAYD){
                 $this->error('付款状态选择错误，保存失败！');
             }

             //开启事务
             M()->startTrans();
             $data = array(
                            'order_id'=>$item['id'],
                            'sn'=>$item['sn'],
                            'uid'=>UID,
                            'comment'=>time_format().'管理用户：'.session('user_auth.username').'将订单：'.$item['sn'].'设置为已付款状态！',
                            'type'=>1,
                            'create_time'=>time()
                        );
             $order_logs_id = M('order_logs')->add($data);
             
             $data = array(
                 'status'=>OrdersService::STATUS_WATI_ACTIVE,
                 'pay_status'=>OrdersService::PAY_STATUS_PAYD,
                 'pay_time'=>time_format(),
                 'update_time'=>time_format()
             );
             $up_f = $this->model->where(array('sn'=>$item['sn']))->save($data);
             if($order_logs_id && $up_f){
                 M()->commit();
             }else{
                 M()->rollback();
                 $this->error('订单付款状态修改失败，请重新再试！');
             }
             $this->success('修改成功',U('detail',array('sn'=>$item['sn'])));
        }
    }
}